package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Response;

@RestController
@RequestMapping("shopping")
@Anoymous
public class CartController {
    @Reference(check = false)
    ICartService iCartService;

    //获得购物车列表
    @GetMapping("carts")
    public ResponseData getCartList(HttpServletRequest request) {
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
//      cartListByIdRequest.setUserId(Long.parseLong("38"));
        CartListByIdResponse cartListById = iCartService.getCartListById(cartListByIdRequest);
        if (!cartListById.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(cartListById.getMsg());
        }
        return new ResponseUtil().setData(cartListById);
    }

    //添加商品到购物车
    @PostMapping("carts")
    public ResponseData addItemToCart(@RequestBody AddCartRequest addCartRequest) {
        AddCartResponse addCartResponse = iCartService.addToCart(addCartRequest);
        if (!addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
        }
        return new ResponseUtil().setData(addCartResponse.getMsg());
    }

    // 更新购物车中商品
    @PutMapping("carts")
    public ResponseData updateCart(@RequestBody UpdateCartNumRequest request) {
        UpdateCartNumResponse updateCartNumResponse = iCartService.updateCartNum(request);
        if (!updateCartNumResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());
        }
        return new ResponseUtil().setData(updateCartNumResponse);
    }

    //删除购物车中商品
    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteItemCart(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setItemId(pid);
        deleteCartItemRequest.setUserId(uid);
        DeleteCartItemResponse deleteCartItemResponse = iCartService.deleteCartItem(deleteCartItemRequest);
        if (!deleteCartItemResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());
        }
        return new ResponseUtil().setData(deleteCartItemResponse);
    }

    //删除购物车中选中的商品
    @DeleteMapping("items/{id}")
    public ResponseData deleteItemCartByChecked(@PathVariable("id") Long id) {
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(id);
        DeleteCheckedItemResposne deleteCheckedItemResposne = iCartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (!deleteCheckedItemResposne.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(deleteCheckedItemResposne.getMsg());
        }
        return new ResponseUtil().setData(deleteCheckedItemRequest);
    }

}